package com.spark.Service.impl;

import com.spark.Service.DiaryService;
import com.spark.mapper.DiaryMapper;
import com.spark.mapper.YearMapper;
import com.spark.pojo.Diary;
import com.spark.pojo.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DiaryServiceImpl implements DiaryService {

    @Autowired
    DiaryMapper diaryMapper;
    @Autowired
    YearMapper yearMapper;

    @Override
    public List<Diary> selectByConditions(Diary diary, String date) {
//        String[] split1 = diary.getTags().split(",");
//        List<Tag> tags = new ArrayList<>();
//        for(String tagId : split1) {
//            tags.add(new Tag(tagId));
//        }
        if(diary.getContent() != null && diary.getContent().equals("")) {
            diary.setContent(null);
        }
        String[] split = date.split("-");
        int[] date1 = new int[split.length];
        for(int i=0; i<split.length; i++) {
            date1[i] = Integer.parseInt(split[i]);
        }
        List<Diary> diaries = diaryMapper.selectByCondition(diary, date1[0], date1[1], date1[2]);
        return diaries;
    }

    @Override
    public List<Diary> selectAllByUserId(String id) {
        return diaryMapper.selectAllWithTagsByUserId(id);
    }

    @Override
    public List<Integer> getYearsByUserId(String id) {
        List<Map<String, Object>> disYearByUserId = yearMapper.getDisYearByUserId(id);
        List<Integer> years = new ArrayList<>();
        for(Map<String, Object> map : disYearByUserId) {
            years.add((Integer) map.get("year"));
        }
        return years;
    }

    @Override
    public Diary getDiaryByDiaryId(String diaryId) {
        return diaryMapper.selectDiaryInfoByDiaryId(diaryId);
    }
}
